#ifndef TCP_H
#define TCP_H

#include <QWidget>

#include <QTcpServer>
#include <QTcpSocket>

#include <iostream>
#include <QGridLayout>

#define log(fmt,...)    qDebug("\033[42m[%s] %s:%d\033[0m--"#fmt"", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define err(fmt,...)    qDebug("\033[41m[%s] %s:%d\033[0m--"#fmt"", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)

#define 		A_POST_CREATE_HOUSE				0xE1
#define 		A_JOIN_SERVER_ID                0xE2
#define 		A_JOIN_SERVER_SUCCEED_ID        0xE3
#define 		A_POST_GET_HOUSE                0xE4
#define 		A_JOIN_ROOM_ID                  0xE5
#define 		A_JOIN_ROOM_SUCCEED_ID          0xE6
#define 		A_TELL_DATA                     0xE7
#define 		A_TELL_STRING                   0xE8
#define 		A_TELL_POS_XY                   0xE9
#define 		A_TELL_COLOR                    0xEA
#define 		A_TELL_UNDO                     0xEC
#define 		A_TELL_UNDORX                   0xED
#define 		A_GET_VERSION                   0xEB

#define 		A_EXIT_SERVER_ID                0xFD
#define 		A_EXIT_ROOM_ID                  0xFE

class tcp: public QWidget
{
    Q_OBJECT
private slots:
    void onReadMessage();   //处理服务端反馈的数据
    void connected();

private:
    uint8_t step;
    uint8_t cnt;
    uint8_t buff[100];
    uint8_t rxlen;
    void DeerSwitch(void);
    void DeerParsing(void);

public:
    uint8_t linkSever();

public:
    void analysis(uint8_t date);
    void tx(uint8_t fun, uint8_t *date, uint8_t datelen);
    void tx(uint8_t fun, uint16_t fd, uint8_t *date, uint8_t datelen);
    void tx_uint8(uint8_t fun, uint8_t date);
    void tx_uint16(uint8_t fun, uint16_t date);

public:
    //QString host = "192.168.88.129";
    QString host = "124.71.7.74";
    quint16 post = 3305;
    QTcpSocket* tcpSocket;  //tcp客户端
public:
    tcp(QWidget *parent = nullptr);

signals:
    void get_id(uint16_t id);
    void creater_succeed();
    void list_addItem(uint16_t fd, QString str);
    void join_succeed(uint16_t tell_id);
    void rx_string(QString str);
    void rx_pos_xy(uint8_t x, uint8_t y, char color);
    void rx_color(char color);
    void rx_version(uint8_t version);
    void rx_undo(void);
    void rx_undo_state(uint8_t state);
};

#endif // TCP_H
